#include "mytextedit.h"
#include "ui_mytextedit.h"
#include"QString"
#include"QDebug"
#include<QFontDialog>
#include <QFileDialog>
#include <QMessageBox>
MytextEdit::MytextEdit(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MytextEdit)
{
    ui->setupUi(this);
    initConnect();
    initFont();
    initHighlight();
    HighLightCurrentLine();

}

MytextEdit::~MytextEdit()
{
    delete ui;
}

void MytextEdit::initConnect()
{
    connect(ui->textEdit->horizontalScrollBar(),&QScrollBar::valueChanged,this,[=](){
        ui->horizontalScrollBar->setValue(ui->textEdit->horizontalScrollBar()->value());
        ui->horizontalScrollBar->setPageStep(ui->textEdit->horizontalScrollBar()->pageStep());
        ui->horizontalScrollBar->setMaximum(ui->textEdit->horizontalScrollBar()->maximum());
        ui->horizontalScrollBar->setMinimum(ui->textEdit->horizontalScrollBar()->minimum());
    });
    connect(ui->horizontalScrollBar,&QScrollBar::valueChanged,this,[=](){
        ui->textEdit->horizontalScrollBar()->setValue(ui->horizontalScrollBar->value());
    });

    connect(ui->textEdit,&QTextEdit::textChanged,this,[=](){
        int linecountOftextEdit = ui->textEdit->document()->lineCount();
        QString text = ui->textBrowser->toPlainText();
        int linecountOftextBrowser = text.trimmed().split("\n").size();

        if(linecountOftextBrowser>linecountOftextEdit)
        {
            for(int i = linecountOftextBrowser;i>linecountOftextEdit;i--)
            {
                text.chop((QString::number(i)+"\n").length());
            }
        }else if(linecountOftextBrowser == 1 && text.length()<1)
        {
            text = "1\n";
        }
        else if(linecountOftextBrowser<linecountOftextEdit)
        {
            for(int i = linecountOftextBrowser;i<linecountOftextEdit;i++)
            {
                text += QString::number(i+1)+"\n";
            }
        }
        ui->textBrowser->setMaximumWidth(25+QString::number(linecountOftextEdit).length()*10);
        ui->textBrowser->setText(text);
    });

    connect(ui->textBrowser->verticalScrollBar(),&QScrollBar::valueChanged,this,[=](){
        ui->textEdit->verticalScrollBar()->setValue(ui->textBrowser->verticalScrollBar()->value());
    });
    connect(ui->textEdit->verticalScrollBar(),&QScrollBar::valueChanged,this,[=](){
        ui->textBrowser->verticalScrollBar()->setValue(ui->textEdit->verticalScrollBar()->value());
    });
    connect(ui->textEdit,&QTextEdit::cursorPositionChanged,this,&MytextEdit::HighLightCurrentLine);
}

void MytextEdit::initFont()
{
    QFont font("Consolas",14);
    ui->textEdit->setFont(font);
    ui->textBrowser->setFont(font);
}

void MytextEdit::initHighlight()
{
    new MyHighlight(ui->textEdit->document());
}

void MytextEdit::HighLightCurrentLine()
{
    QList<QTextEdit::ExtraSelection> extraSelections;
    QTextEdit::ExtraSelection selection;
    selection.format.setBackground(QColor(0,100,100,20));
    selection.format.setProperty(QTextFormat::FullWidthSelection,true);
    selection.cursor = ui->textEdit->textCursor();
    extraSelections.append(selection);

    ui->textEdit->setExtraSelections(extraSelections);
}

void MytextEdit::setText(QString file)
{
    ui->textEdit->setText(file);
}

QString MytextEdit::getText()
{
    return ui->textEdit->toPlainText();
}

bool MytextEdit::savefile()
{
    QString filename;
    if(curFile.isEmpty())
    {
        filename = QFileDialog::getSaveFileName(this,"选择保存的文件");
        curFile = filename;
    }
    else
    {
        filename = curFile;
    }
    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        QMessageBox::warning(this,"警告","保存文件失败 \n"+file.errorString());
        return  false;
    }
    QTextStream out(&file);
    out<<ui->textEdit->toPlainText();
    file.close();
    return true;
}

bool MytextEdit::saveAsFile()
{
    QString filename = QFileDialog::getSaveFileName(this,"选择保存的文件");
    curFile = filename;
    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        QMessageBox::warning(this,"警告","保存文件失败 \n"+file.errorString());
        return false;
    }
    QTextStream out(&file);
    QString text = ui->textEdit->toHtml();
    out<<text;
    file.close();
    return true;
}

void MytextEdit::setfilename(QString filename)
{
    curFile = filename;
}

QString MytextEdit::getfilename()
{
    return curFile;
}

void MytextEdit::setCopy()
{
    ui->textEdit->copy();
}

void MytextEdit::setPaste()
{
    ui->textEdit->paste();
}

void MytextEdit::setCut()
{
    ui->textEdit->cut();
}

void MytextEdit::setunderline(bool check)
{
    ui->textEdit->setFontUnderline(check);
}

void MytextEdit::setItalics(bool checked)
{
    ui->textEdit->setFontItalic(checked);
}

void MytextEdit::setUndo()
{
    ui->textEdit->undo();
}

void MytextEdit::setRedo()
{
    ui->textEdit->redo();
}

void MytextEdit::setBoler(bool checked)
{
    ui->textEdit->setFontWeight(checked?QFont::Bold:QFont::Normal);
}

void MytextEdit::setFont()
{
    bool ret = false;
    QFont font = QFontDialog::getFont(&ret,this);
    if(ret)
    {
        ui->textEdit->setFont(font);
        //ui->textBrowser->setMaximumWidth(font.pointSize());
    }
}


